package com.daohang361.imqq.Asycore;

import static org.apache.commons.codec.digest.DigestUtils.md5;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import com.daohang361.imqq.bean.FrindsInfor;
import com.daohang361.imqq.tool.Hex2Bytes;
import com.daohang361.imqq.tool.MyUtil;

public class QQUser {
	private boolean login=false;
	private int qq;
	private String passwd;
	private byte[] md51;
	/**
	 * 存放QQ密码2次特殊md5加密后文本
	 */
	private byte[] md52;
	private String sessionkey;
	/**
	 * 用于记录登陆过程中流程
	 */
	private int logintimes;
	private byte[] send;
	private int command;
	private int pakage;
	private int sendPacke=0;
	private int recive=0;
	
	private ArrayList<byte[]> directSend = new ArrayList<byte[]>(200);
	
	/**
	 * 好友资料
	 */
	private HashMap<Integer,FrindsInfor> frindsInforMap = new HashMap<Integer,FrindsInfor>();
	/**
	 * 好友分组
	 */
	private Hashtable<Integer,byte[]> friendGroupMap = new Hashtable<Integer,byte[]>();
	/**
	 *  <pre>
	 * 存放QQ好友列表
	 * 格式为:
	 * 好友组1（索引）=好友组1下所有好友(List)
	 * 好友组2（索引）=好友组2下所有好友(List)
	 * ....
	 * </pre>
	 */
	private Hashtable<Integer, ArrayList<Integer>> friendMap = new Hashtable<Integer,ArrayList<Integer>>();
	/**
	 * 用于存放QQ的属性,以及临时key,tokenData之类
	 */
	private Map<String, byte[]> attrbute = new HashMap<String, byte[]>();
	
	/**
	 *  <pre>
	 *  初始化QQ，包括一次md5密码，第2次md5特殊处理密码。初始化登陆包为256+(0-10000)的随机数,因为登陆包
	 *  
	 *  为2个字节故最大长度应该为16*16*16*16-1,加256的 目的为使int型登陆包至少为2个字节，避免256以下的
	 * 
	 *  数字转换16进制后长度只有2个字节
	 *  
	 *  </pre>
	 */
	public void init()
	{
		this.setLogintimes(0);//设置成功登陆step是接受到服务器返回登陆包后
		byte[] md5One= md5(this.getPasswd().getBytes());//DF 95 8D ED CB C7 69 22 E4 BF E3 48 32 19 67 91
		byte[] md5QQ=Hex2Bytes.hexStringToBytes("00000000"+MyUtil.intToHex(this.qq));//00000000 66 2B 4F EC
		byte[] md5Two =new byte[md5One.length+md5QQ.length];//DF 95 8D ED CB C7 69 22 E4 BF E3 48 32 19 67 91 00000000 66 2B 4F EC
		System.arraycopy(md5One,0,md5Two,0,md5One.length); 
		System.arraycopy(md5QQ,0,md5Two,md5One.length,md5QQ.length); 
		this.setMd51(md5One);
		this.setMd52(md5(md5Two));//这个是md52次密码36 3E 8C B0 99 03 BC CD A7 EE 0F D7 6B A5 4C 81
		this.setPakage(256+(int) (Math.random()*10000));
	}
	
	/**
	 * 添加好友，用于初始化QQ
	 * @param index
	 * @param list
	 */
	public void addFriendsList(Integer index,ArrayList<Integer> list)
	{
		ArrayList<Integer> oldList = getFriendsList(index);
		oldList.addAll(list);
		friendMap.remove(index);
		friendMap.put(index, oldList);
	}
	
	
	/**
	 * 返回所有好友分组
	 * @return
	 */
	public Set<Integer> getFriendsGroup()
	{
		return friendMap.keySet();
	}
	
	public void addFriend(int index, int firendsQQ) {
		ArrayList<Integer> oldList = getFriendsList(index);
		if(oldList==null)
			oldList=new ArrayList<Integer> ();
		oldList.add(firendsQQ);
		friendMap.remove(index);
		friendMap.put(index, oldList);
		
	}
	/**
	 * 获取索引分组下的所有好友号码
	 * @param key
	 * @return
	 */
	public ArrayList<Integer> getFriendsList(Integer key)
	{
		return friendMap.get(key);
	}
	
	public void removeAttr(String s){
		attrbute.remove(s);
	}
	
	public void addAttr(String s , byte[] b){
		attrbute.remove(s);
		attrbute.put(s, b);
	}
	
	public byte[]  getAttr(String s){
		return attrbute.get(s);
	}
	
	public int getPakage() {
		this.setPakage(256+(int) (Math.random()*10000));
		return pakage;
	}

	public void setPakage(int pakage) {
		this.pakage = pakage;
	}

	public int getLogintimes() {
		return logintimes;
	}
	public void setLogintimes(int logintimes) {
		this.logintimes = logintimes;
	}
	public String getSessionkey() {
		return sessionkey;
	}
	public void setSessionkey(String sessionkey) {
		this.sessionkey = sessionkey;
	}
	public int getQq() {
		return qq;
	}
	public void setQq(int qq) {
		this.qq = qq;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	public byte[] getMd51() {
		return md51;
	}
	public void setMd51(byte[] md51) {
		this.md51 = md51;
	}
	public byte[] getMd52() {
		return md52;
	}
	public void setMd52(byte[] md52) {
		this.md52 = md52;
	}
	public byte[] getSend() {
		return send;
	}
	public void setSend(byte[] send) {
		this.send = send;
	}
	public int getCommand() {
		return command;
	}

	public void setCommand(int command) {
		this.command = command;
	}

	public boolean isLogin() {
		return login;
	}

	public void setLogin(boolean login) {
		this.login = login;
	}
	public void addFriendGroup(int index,byte[] name){
		friendGroupMap.remove(index);
		friendGroupMap.put(index, name);
	}
	
	public byte[] GetFriendGroupName(int index){
		return friendGroupMap.get(index);
	}

	public Hashtable<Integer, byte[]> getFriendGroupMap() {
		return friendGroupMap;
	}

	/**
	 * 好友资料队列
	 * @return
	 */
	public HashMap<Integer,FrindsInfor> getFrindsInforMap() {
		return frindsInforMap;
	}
	public  void setFrindsInforMap(HashMap<Integer,FrindsInfor> frindsInforMap){
		this.frindsInforMap=frindsInforMap;
	}

	public int getSendPacke() {
		return sendPacke;
	}

	public void setSendPacke() {
		this.sendPacke = sendPacke+1;
	}

	public int getRecive() {
		return recive;
	}

	public void setRecive() {
		this.recive = recive+1;
	}


	public int DirectSendLength() {
		
		return directSend.size();
	}

	public void addDirectSendByte(byte[] e){
		directSend.add(e);
	}
	public synchronized byte[] TokeDirectSend() {
		
		try {
			byte temp[] =directSend.get(0);
			directSend.remove(0);
			return temp;
		} catch (Exception e) {
		}
		return null;
	}
}
